package ContentManagers.Security;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ervin James
 */
public abstract class Validator {
    
    //replaces known characters that contribute to XSS or SQL Injection
    public static String inputSanitizer(String input){
        
        //blacklist
        String filtered = input.replaceAll("(?i)<script.*?>.*?</script.*?>", ""); //<script> are removed 
        filtered = filtered.replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", ""); //javascript: call are removed
        filtered = filtered.replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", ""); //remove on* attributes like onLoad or onClick 
        filtered = filtered.replaceAll("--",""); //prevents comments in SQL
        
        //whitelist
        filtered = filtered.replaceAll("([^A-Za-z0-9@. _-]+)", "");
        
        
        return filtered;
    }
    
        public static String passwordSanitizer(String input) {
             //blacklist
        String filtered = input.replaceAll("(?i)<script.*?>.*?</script.*?>", ""); //<script> are removed 
        filtered = filtered.replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", ""); //javascript: call are removed
        filtered = filtered.replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", ""); //remove on* attributes like onLoad or onClick 
        filtered = filtered.replaceAll("--",""); //prevents comments in SQL
        
        //remove whitespace
        filtered = filtered.replaceAll("\\s","");
        
        return filtered;
    }
          
    public static boolean isValidEmail(String input) {
        boolean isValid=false;
        
        //esapi
        String expression = "^[A-Za-z0-9._%'-]+@[A-Za-z0-9.-]+\\.[a-zA-Z]{2,4}$";    
        
        //Make the comparison case-insensitive.  
        Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
        Matcher matcher = pattern.matcher(input);  
        if(matcher.matches()){  
        isValid = true;  
        }  

        
        return isValid;  
        
    }
    
    public static boolean isCreditCardValid(String number, int type) {
        boolean isValid=false;
        number = number.replaceAll(" ", "");
              
        switch(type) {
            
         case 0: //Master Card Sample 5500 0000 0000 0004
         if (number.length() != 16 ||
                Integer.parseInt(number.substring(0, 2)) < 51 ||
                Integer.parseInt(number.substring(0, 2)) > 55)
            {
                return false;
            }
         break;
             
         case 1: //Visa 4111 1111 1111 1111
            if ((number.length() != 13 && number.length() != 16) ||
                    Integer.parseInt(number.substring(0, 1)) != 4)
            {
                return false;
            }
            break;    
             
        }
       
        return luhnValidate(number);
    }
    
    //standard algorithm for validating credit card numbers
     private static boolean luhnValidate(String numberString) {
        char[] charArray = numberString.toCharArray();
        int[] number = new int[charArray.length];
        int total = 0;
		
        for (int i=0; i < charArray.length; i++) {
            number[i] = Character.getNumericValue(charArray[i]);
        }
		
        for (int i = number.length-2; i > -1; i-=2) {
            number[i] *= 2;
			
            if (number[i] > 9)
                number[i] -= 9;
        }
		
        for (int i=0; i < number.length; i++)
            total += number[i];
		
            if (total % 10 != 0)
                return false;
		
        return true;
    }
    
     public static int isInteger(String intCheck)
     {
         try{
             Integer.parseInt(intCheck);
                } catch(NumberFormatException nfe) {
                    return -1;
            }
        return Integer.parseInt(intCheck);
     }
    
}
